import {Button, Form, Select, message} from 'antd';
import {ExportOutlined} from '@ant-design/icons';
import React, {useRef, useState} from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import {exportWIPStatus, exportWIPStatusFile, queryWIPStatus} from "@/services/process";
import request from 'umi-request';
import moment from "moment";

function download() {
  request('/api/productionStats/export/WIP', {
    method: 'GET',
    responseType: 'blob',
  }).then(res => {
    console.log(res)
    const blob = new Blob([res]);
    const objectURL = URL.createObjectURL(blob);
    let btn = document.createElement('a');
    btn.download = 'WIP.xlsx';
    btn.href = objectURL;
    btn.click();
    URL.revokeObjectURL(objectURL);
    btn = null;
  });
}

const TableList = () => {
  const actionRef = useRef();
  const getFileName = (fileName)=> {
    const suffix = fileName.split('.')[1];
    let type = null;
    if (suffix === 'xlsx' || suffix === 'xls') {
      type = 'application/msexcel';
    }
    else if (suffix === 'zip') {
      type = 'application/zip';
    }
    else if (suffix === 'docx') {
      type = 'application/docx';
    }
    else if (suffix === 'pdf') {
      type = 'application/pdf';
    }
    return type;
  }
  const  handleExportClick = async () => {
    exportWIPStatusFile({}).then(res => {
      if (res.status === 200) {
        const _res = res;
        const fileName = decodeURI((_res.headers)['content-disposition'].split('=')[1]);
        const suffix = getFileName(fileName);
        const blob = new Blob([_res.data], suffix != null ? {type: suffix} : {});
        if (navigator.userAgent.indexOf('Trident') > -1) {
          window.navigator.msSaveBlob(blob, fileName);
        }
        else {
          const downloadElement = document.createElement('a');
          const href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = fileName; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        }
      }
      else {
        message.error('暂无文件下载')
      }
    })
  }

  const columns = [
    {
      title: '序号',
      dataIndex: 'key',
      hideInForm: true,
      fixed: 'left',
      width: 150,
      hideInSearch: true,
      render: (text, record, index, action) => {
        return `${index + 1}`
      }
    },
    {
      title: '产品编码',
      dataIndex: 'productCode',
      fixed: 'left',
      width: 150,
    },
    {
      title: '订单编号',
      dataIndex: 'orderCode',
      fixed: 'left',
      width: 150,
    },
    {
      title: '生产编号',
      dataIndex: 'productionNo',
      fixed: 'left',
      width: 150,
    },
    {
      title: '下单日期',
      dataIndex: 'receivedTime',
      fixed: 'left',
      width: 150,
      valueType: 'dateTime',
      hideInSearch: true,
      hideInForm: true,
      render: (text, record) => {
        return moment(record.receivedTime, 'YYYY-MM-DD HH:mm:ss').format('YYYY-MM-DD')
      }
    },
    {
      title: '交货日期',
      dataIndex: 'requirementDeliveryTime',
      fixed: 'left',
      valueType: 'dateTime',
      width: 150,
      hideInSearch: true,
      hideInForm: true,
      render: (text, record) => {
        return moment(record.requirementDeliveryTime, 'YYYY-MM-DD HH:mm:ss').format('YYYY-MM-DD')
      }
    },
    {
      title: '订单数量',
      dataIndex: 'orderNum',
      fixed: 'left',
      width: 150,
      hideInSearch: true,
    },
    {
      title: '计划投产量',
      dataIndex: 'productionNum',
      fixed: 'left',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '层数',
      dataIndex: 'pcbLayerNum',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '板材类型',
      dataIndex: 'pcbType',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '单元长度(mm)',
      dataIndex: 'panelLength',
      hideInSearch: true,
      width: 200,
    },
    {
      title: '单元宽度(mm)',
      dataIndex: 'panelWidth',
      hideInSearch: true,
      width: 200,
    },
    {
      title: '开料',
      dataIndex: 'p100clmn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '内层线路',
      dataIndex: 'p100ilnn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '内层蚀刻',
      dataIndex: 'p100iecn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '内层检验',
      dataIndex: 'p100iaon',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '内层氧化',
      dataIndex: 'p100ioxn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '层压',
      dataIndex: 'p100lmnn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '板电',
      dataIndex: 'p100plen',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '外层钻孔',
      dataIndex: 'p100odrn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '一次铣槽（孔）',
      dataIndex: 'p100ostn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '沉铜板电',
      dataIndex: 'p100cpen',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '外层图形',
      dataIndex: 'p100optn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '图形电镀',
      dataIndex: 'p100pten',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '退膜蚀检',
      dataIndex: 'p100uean',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '外层蚀检',
      dataIndex: 'p100oean',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '丝印阻焊',
      dataIndex: 'p100sssn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '丝印字符',
      dataIndex: 'p100sscn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '喷锡',
      dataIndex: 'p100hasn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '沉镍金',
      dataIndex: 'p100inan',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '碳油',
      dataIndex: 'p100cikn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '二次钻孔',
      dataIndex: 'p100sdrn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '成形',
      dataIndex: 'p100fomn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: 'V-CUT',
      dataIndex: 'p100vctn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '测试',
      dataIndex: 'p100tstn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: 'FQC',
      dataIndex: 'p100fqcn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '蓝胶',
      dataIndex: 'p100pbmn',
      hideInSearch: true,
      width: 150,
    },
    {
      title: 'FQA',
      dataIndex: 'p100fqan',
      hideInSearch: true,
      width: 150,
    },
    {
      title: '包装入库',
      dataIndex: 'p100pacn',
      hideInSearch: true,
      width: 150,
    },
  ];

  return (
    <PageContainer>
      <ProTable
        actionRef={actionRef}
        rowKey="orderCode"
        search={{
          labelwidth: 150,
        }}
        sticky
        scroll={{x: 3300}}
        toolBarRender={() => [
          <Button type="primary" onClick={handleExportClick}>
            <ExportOutlined/> 导出文件
          </Button>,
        ]}
        request={async (params, sorter, filter) => {
          const res = await queryWIPStatus(params)
          return {
            data: res.data,
            success: true,
            total: res.data.length
          }
        }}
        columns={columns}
      />
    </PageContainer>
  );
};

export default TableList;
